import request from '@/util/request'
import md5 from 'js-md5'
import { referenceIdKey } from '@/js/constant'
import { promoterKey } from '@/js/constant'

export function sendEmail(headers, email) {
  return request({
    url: `/code/company/register/${email}.json`,
    method: 'get',
    headers,
    baseURL: process.env.VUE_APP_AJS_USER
  })
}

export function next(headers, code) {
  return request({
    url: `/register/companyNext/${code}.json`,
    method: 'post',
    headers,
    baseURL: process.env.VUE_APP_AJS_USER
  })
}

export function step(headers, data) {
  return request({
    url: '/register/companyStep.json',
    method: 'post',
    data,
    headers,
    baseURL: process.env.VUE_APP_AJS_USER
  })
}

export function companyRegister(headers, data) {
  data.userPassword = md5(data.userPassword)
  const referenceId = window.sessionStorage.getItem(referenceIdKey)
  if (referenceId) {
    headers.referenceId = referenceId
  }
  const promoter = window.sessionStorage.getItem(promoterKey)
  if (promoter) {
    Object.assign(headers, JSON.parse(promoter))
  }
  return request({
    url: '/register/company.json',
    method: 'post',
    data,
    headers,
    baseURL: process.env.VUE_APP_AJS_USER
  })
}
